package com.neusoft.bigdata.hbase.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

/**
 * @author lihzh
 * @date 2012-11-14 下午4:51:22
 */
public class CommonFileCreator {

	private static final String PATH = "F:/2000-";
	// 2011年11月1日
	public static final long STARTTIME = getTime(2011, 10, 1, 0, 0);
	// 2012年11月1日
	public static final long ENDTIME = getTime(2012, 10, 1, 0, 0);
	public static final long INTERVAL = 1 * 60 * 1000L;
	public static final String ID_PREFIX = "XXXXXXXX-XXXX-XXXX-XXXX-";

	/**
	 * @param args
	 * @author lihzh
	 * @throws IOException
	 * @date 2012-11-14 下午4:51:22
	 */
	public static void main(String[] args) throws IOException {
		int fileCount = 10;
		for (int k = 1; k <= fileCount; k++) {
			try {
				long startTime = System.currentTimeMillis();
				long timeCount = 24 * 60 * 5;
				int resourceCount = 2000;
				FileWriter writer = null;
				long timeStamp = STARTTIME;
				File dataFile = new File(PATH + k + ".txt");
				if (dataFile.exists()) {
					continue;
				}
				try {
					dataFile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
				writer = new FileWriter(dataFile);
				for (int i = 1; i <= resourceCount; i++) {
					String id = ID_PREFIX + createId(i);
					for (long j = 0; j < timeCount; j++) {
						timeStamp += INTERVAL;
						String rowKey = id + "_" + timeStamp;
						Random random = new Random();
						StringBuilder builder = new StringBuilder();
						builder.append(rowKey).append("\t");
						for (int l = 0; l < 20; l++) {
							String value = String.valueOf(random.nextDouble())
									.substring(0, 4);
							builder.append(value).append("\t");
						}
						builder.append(id).append("\t")
								.append(timeStamp).append("\t");
						writer.append(builder.toString());
						if ((i + 1) * (j + 1) < timeCount * resourceCount) {
							writer.append("\r");
						}
					}
				}
				long endTime = System.currentTimeMillis();
				System.out.println("Cost Time: " + (endTime - startTime));
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
		}
	}

	/**
	 * 生成Id
	 * @param resourceCount
	 * @return
	 * @author lihzh
	 * @date 2013-3-27 下午3:27:59
	 */
	private static String createId(int resourceCount) {
		DecimalFormat format = new DecimalFormat();
		format.setMinimumIntegerDigits(12);
		format.setGroupingUsed(false);
		return format.format(resourceCount);
	}

	/**
	 * Get a new file.
	 * 
	 * @return
	 * @author lihzh
	 * @param fileCount
	 * @date 2012-11-14 下午4:53:31
	 */
	private static File getFile(String fileCount) {
		File file = new File(PATH + fileCount + ".txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	protected static long getTime(int year, int month, int day, int hour,
			int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day, hour, minute, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime().getTime();
	}
}
